package Servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import util.JDBCUtil;

@WebServlet(name = "Servletjdbc", value = "/Servletjdbc")
public class Servletjdbc extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.getParameter("index.jsp");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
           statement= JDBCUtil.getConnection().prepareStatement("select * from goods");
//           PreparedStatement statement2= JDBCUtil.getConnection().prepareStatement("insert into goods values (1009,'乐事薯片',6)");
           resultSet = statement.executeQuery();
//           statement2.executeLargeUpdate();
            while (resultSet.next()){
                out.println("商品编号:"+resultSet.getInt("id")+"   商品名:"+resultSet.getString("goods_name")
                +"  价格:"+resultSet.getString("goods_price")+"元");
                out.println("<br/>");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
          finally {
            if(null!=resultSet){
                try {
                    resultSet.close();
                } catch (SQLException exception) {
                    exception.printStackTrace();
                }
            }
            if(null!=statement){
                try {
                    statement.close();
                } catch (SQLException exception) {
                    exception.printStackTrace();
                }
            }
        }



    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
